<?php
    date_default_timezone_set('UTC');
?>
<div class="well" >
    <div class="row-fluid centrar">
        <h3>Lista de proveedores</h3>
        <input type="hidden" name="id" value=""/>
    </div><br/>
    <div class="contenido contexto">
        <div id="grid"></div>
    </div>
    <br/>      
</div>
<script type="text/x-kendo-template" id="template">
    <div class="toolbar contexto">
        <input type="button" class="k-button" name="btnNew" data-bind="click: new" value="Nuevo"/>


    </div>
</script>
<script type="text/javascript">
    $(document).ready(function(){

    	var dsGrid = new kendo.data.DataSource({
            type: "json",
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "<?php echo $view->url(array('controller' => 'Providers', 'action' => 'getListProvider')); ?>",
                        success: function (resultado){
                            options.success(resultado);
                        }
                    });
                }
            }, 
            schema: {
                model: {
                    id: "idProvider",
                    fields: {
	                         empresa: { type: "string" },
	                         alias: { type: "string" },
	                         rfc: { type: "string" },
	                         director: { type: "string" },
	                         mail: { type: "string" },
	                         address: { type: "string"}
                    }
                }
            },
            pageSize: 10
        });

        $("#grid").kendoGrid({
            dataSource: dsGrid,
            height: 430,
            scrollable: true,
            filterable: {
                extra: false
            },
            toolbar: kendo.template($("#template").html()),
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 10
            },
            selectable: "row",
            columns: [
                { field: "empresa", title: "Empresa", width: "200px" },
                { field: "alias", title: "Alias", width: "120px" },
                { field: "rfc", title: "RFC", width: "200px" },
                { field: "director", title: "Director", width: "200px" },
                { field: "mail", title: "Correo", width: "200px" },
                { field: "address", title: "Direccion", width: "200px" },
                { field: "", template: '<input type="button" class="k-button" name="" onclick="editProvider()" value="Editar"/><input type="button" class="k-button" name="btnDelete" onclick="delProvider()" value="Eliminar"/>', width: "180px" }
				]
        });
        
       
        var viewModel = kendo.observable({
        	proveedoresId: 0,
            new: function(){
                this.set("proveedoresId", 0);
                window.open("<?php echo $view->url(array('controller' => 'Providers', 'action' => 'frmProvider')); ?>", "_self");
            }
         }); 
        
        kendo.bind($(".contexto"), viewModel);
    });

    function delProvider()
    {
    	 //llamar al id del registro
         var grid = $("#grid").data("kendoGrid");
        if (grid.dataItem(grid.select()) != undefined)
            {
        var idprovider = grid.dataItem(grid.select()).idProvider
            }

        else
        	{
        		alert("No seleccionado")
        		return false;
            }
        //llamando al controlador bankaccounts
            $.ajax({

              type: "POST",
              data: 'idprovider=' + idprovider,
              url: "<?php echo $view->url(array('controller' => 'Providers', 'action' => 'delProvider')); ?>",
              success: function (respuesta)
                {
            		alert("Registro eliminado")
                }
           });
            window.location.reload();
      }
    
    function editProvider()
    {
        var grid = $("#grid").data("kendoGrid");
        if (grid.dataItem(grid.select()) != undefined)
            {
        var idprovider = grid.dataItem(grid.select()).idProvider
            }

        else
        	{
        		alert("No seleccionado")
        		return false;
            }
        window.open("<?php echo $view->url(array('controller' => 'Providers', 'action' => 'editProvider')).'idprovider/'; ?>" + idprovider, "_self");
    }
</script>